package com.dayaokeji.rhythmschoolstudent.wiget

import android.os.Bundle
import android.support.v7.app.AppCompatDialogFragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.utils.PermissionUtil

/**
 *********************************************************************
 **  Created by Zeno on 2018/7/24
 *********************************************************************
 **  version 1.0
 *********************************************************************
 **  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 *********************************************************************
 ** 权限提示dialog
 *********************************************************************
 */
class PermissionTipsDialog: AppCompatDialogFragment(), View.OnClickListener {

    companion object {
        private const val EXTRA_PERMISSIONS = "permission"

        fun newInstance(permissions:Array<String>): PermissionTipsDialog {
            val bundle = Bundle()
            val permissionTipsDialog = PermissionTipsDialog()
            bundle.putStringArray(EXTRA_PERMISSIONS,permissions)
            permissionTipsDialog.arguments = bundle
            return permissionTipsDialog
        }
    }

    private var tvPermissionTips: TextView? = null
    private var permissions: Array<String>? = null
    private var settingClickListener: SettingClickListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.dialog_permission_tips, container, false)
        tvPermissionTips = rootView.findViewById(R.id.tv_permission_tips)
        rootView.findViewById<TextView>(R.id.tv_cancel_dialog).setOnClickListener(this)
        rootView.findViewById<TextView>(R.id.tv_to_setting).setOnClickListener(this)
        return rootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        arguments?.let {
            permissions = it.getStringArray(EXTRA_PERMISSIONS)
        }

        permissions?.let {
            val permissionName = PermissionUtil.getPermissionName(activity, it)
            val tips = "当前没有获取到\n${permissionName}\n等权限请到设置中设置，以便后续操作。"
            tvPermissionTips?.text = tips
        }
    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                R.id.tv_cancel_dialog -> {
                    dismiss()
                }
                R.id.tv_to_setting -> {
                    settingClickListener?.setting()
                    dismiss()
                }
            }
        }
    }

    fun setSettingClickListener(settingClickListener: SettingClickListener){
        this.settingClickListener = settingClickListener
    }

    interface SettingClickListener{
        fun setting()
    }
}